package com.medical.medical.service;

import com.medical.medical.entity.Department;
import com.medical.medical.entity.*;
import com.medical.medical.mapper.*;
import com.medical.medical.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DoctorServiceImpl
 * @Description 医生-接口实现类
 * @Author WLD
 * @Date 2022/9/14 8:56
 * @Version 1.0
 */
@Service
@Slf4j
public class DoctorServiceImpl implements DoctorService{

    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private BookingMapper bookingMapper;

    @Resource
    private WorkForcesMapper workForcesMapper;

    /**
     * @Description: 医生验证登录
     * @author: 喜杨杨
     * @date: 2022/9/21 17:08
     * @param: [phone, password]
     * @return: com.medical.medical.entity.Doctor
     **/
    @Override
    public Doctor login(String phone, String password) {
        Doctor doctor = null;
        try {
            doctor = doctorMapper.getLoginUser(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //匹配密码
        if(null != doctor){
            if(!doctor.getPassword().equals(password)) {
                doctor = null;
            }
        }
        return doctor;
    }

    @Override
    public List<Doctor> getDoctorByDepartmentId(int departmentId) {
        List<Doctor> doctors = doctorMapper.getDoctorByDepartmentId(departmentId);
        for (Doctor doctor : doctors) {
            System.out.println(doctor);
        }
        return doctors;
    }

    @Override
    public boolean addrecord(Record record) {
        boolean flag = false;
        if (doctorMapper.addrecord(record)>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean adddrug(int recordId, List<DrugNumberAndId> drugNumberAndIdList ) {
        boolean flag = false;
        if (doctorMapper.adddrug(recordId,drugNumberAndIdList)>0){
            flag = true;
        }
        return flag;
    }

    @Override
     /**
      *@author WangLuDong
      *@Description 方法描述 选择科室,医生进行预约挂号
      *@Date 2022-09-19 12:15:05
       * @param workforcesDepartment
      * @param workforcesDoctor
      *@Return {@link Map< String, Object>}
      */
     //int workforcesDepartment, int workforcesDoctor
    public Map<String, Object> doBookingBydepartmentNameAndDName(int workforcesDepartment, int workforcesDoctor) {
        log.info("输入的科室名ID:{}",workforcesDepartment);
        log.info("输入的医生名ID:{}",workforcesDoctor);
        //判断医生排班表里的剩余挂号数
        int egistration = this.judgeEgistrationByDName(workforcesDoctor);
        if (egistration > 0){
            //修改医生剩余号数
            int resultInfo = modifyEgistration(egistration);
            if (resultInfo > 0){
                log.info("修改剩余号数结果:{}",(resultInfo > 0));
                return ResultUtil.success("预约挂号成功");
            }
            return ResultUtil.error("修改剩余号数失败,导致预约挂号失败");
        }else if (egistration == 0){
            return ResultUtil.successNull("医生剩余号数为0,不可预约");
        }
        return ResultUtil.error("程序异常");
    }

    /**
     * 查询医生所有信息
     * @param departmentId
     * @return
     */
    @Override
    public List<Doctor> getDocListByDepartmentId(Integer departmentId) {
        List<Doctor> doctors = doctorMapper.getDocListByDepartmentId(departmentId);
        return doctors;
    }

    /**
      * @description 查看就诊人
      * @author ChenLi
      * @date 2022-09-26 23:10:22
      * @param dId
     * @param department
      * @return {@link List< Patient>}
      */
    @Override
    public List<Patient> queryPatient(int dId,int department) {
        List<Patient> patientList = doctorMapper.queryPatient(dId,department);
        return patientList;
    }

    /**
      * @description 获得科室
      * @author ChenLi
      * @date 2022-09-27 00:31:17
      * @param doctorId
      * @return {@link String}
      */
    @Override
    public String getDepartment(int doctorId) {
        String departmentName = doctorMapper.getDepartment(doctorId);
        return departmentName;
    }

    /**
      *@author WangLuDong
      *@Description 方法描述 剩余号数-1
      *@Date 2022-09-19 12:10:31
       * @param egistration
      *@Return {@link null}
      */
    private int modifyEgistration(int egistration) {
        return workForcesMapper.modifyEgistration(egistration);
    }

    /**
     * WLD
     * 根据医生id获取医生排班表里的剩余挂号数
     */
    private int judgeEgistrationByDName(int workforcesDoctor) {
        Integer egistration = workForcesMapper.getEgiestration(workforcesDoctor);
        log.info("该医生剩余号数:{}",egistration);
        if (egistration != null){
            return egistration;
        }
        return 0;
    }

    @Override
    public List<String> getDrugNum(String drugName) {
        List<String> drugNumList = null;
        drugNumList = doctorMapper.getDrugNum(drugName);
        if (drugNumList != null && drugNumList.isEmpty() ){
            log.info("查询药品编号失败");
        }else {
            log.info("查询药品编号成功");
        }
        return drugNumList;
    }

    @Override
    public Drug getDrug(String drugName, String drugNum) {
        Drug drug = doctorMapper.getDrug(drugName,drugNum);
        return drug;
    }
}
